﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.Login
{
    public partial class passwordRecovery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
            }
        }

        protected void btnPasswordRecovery_Click(object sender, EventArgs e)
        {
            PasswordRequest passReq = dbLogin.RequestPasswordRecovery(txtPasswordRecovery.Text);

            if (passReq.UserExists)
            {
                SendPasswordResetEmail(passReq.Email, txtPasswordRecovery.Text, passReq.UniqueId.ToString());
                lblPasswordRecoveryMessage.Visible = true;
                txtPasswordRecovery.Text = String.Empty;
            }
            else
            {
                lblPasswordRecoveryMessage.Visible = true;
                txtPasswordRecovery.Text = String.Empty;
            }
        }

        /// <summary>
        /// Sends an e-mail containing the password reset link.
        /// </summary>
        /// <param name="toEmail">Specifies the receiver's e-mail.</param>
        /// <param name="userName">Specifies the receiver's user name.</param>
        /// <param name="uniqueId">Spifies the unique ID of the password reset request.</param>
        private void SendPasswordResetEmail(string toEmail, string userName, string uniqueId)
        {
            //Here the SMTP Client is set up with necessary information (host, port, credentials)
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = "InsertSmtpHostHere"; //Example smtp.gmail.com or something@ucnkompetencebors.dk
            smtpClient.Port = 0; //Insert network port to the host here example is 587 for gmail
            smtpClient.Credentials = new NetworkCredential("EmailUserName", "EmailPassword"); //Insert credentials for the smtp server here
            smtpClient.EnableSsl = true;

            //Stringbuilder is used to construct the message body for the mail sent to the users
            StringBuilder sbEmailBody = new StringBuilder();
            sbEmailBody.Append("Kære " + userName + "<br /><br />");
            sbEmailBody.Append("Tryk på nedenstående link, for at nulstille dit password.");
            sbEmailBody.Append("<br />");
            sbEmailBody.Append("http://localhost:12947/Login/resetPassword.aspx?uid=" + uniqueId);
            sbEmailBody.Append("<br /><br />");
            sbEmailBody.Append("UCN Kompetence Børs");

            sbEmailBody.Append("<br /><br />");

            sbEmailBody.Append("Dear " + userName + "<br /><br />");
            sbEmailBody.Append("Please click on the following link to reset your password.");
            sbEmailBody.Append("<br />");
            sbEmailBody.Append("http://localhost:12947/Login/resetPassword.aspx?uid=" + uniqueId);
            sbEmailBody.Append("<br /><br />");
            sbEmailBody.Append("UCN Kompetence Børs");

            //Insert the e-mail from which the message should be sent instead of "FromEmail"
            MailMessage msg = new MailMessage("FromEmail", toEmail);
            msg.Body = sbEmailBody.ToString();
            msg.IsBodyHtml = true;
            msg.Subject = "Password nulstilling / reset";

            smtpClient.Send(msg);
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnPasswordRecovery.Text = GetLocalResourceObject("btnPasswordRecoveryText").ToString();
            lblPasswordRecoveryText.Text = GetLocalResourceObject("lblPasswordRecoveryTextText").ToString();
            lblPasswordRecoveryHeader.Text = GetLocalResourceObject("lblPasswordRecoveryHeaderText").ToString();
            lblPasswordRecoveryUserName.Text = GetLocalResourceObject("lblPasswordRecoveryUserNameText").ToString();
            lblPasswordRecoveryMessage.Text = GetLocalResourceObject("lblPasswordRecoveryMessageText").ToString();

            string userNameValidate = GetLocalResourceObject("rfvalUsernameText").ToString();
            rfvalUsername.ToolTip = userNameValidate;
            rfvalUsername.ErrorMessage = userNameValidate;
        }
    }
}